from sklearn import datasets
import pandas as pd
import numpy as np
# import  matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.cluster import AgglomerativeClustering
from numpy import mean

def getpcadata(l):
    # 导入数据集
    data = pd.read_excel(r'./myApp/forecast/data.xlsx')
    data = np.array(data)
    data_list = data.tolist()
    iris_data = []
    for i in data_list:
        tt = []
        ttt = i[11:18]
        tt.append(max(ttt))
        tt.append(min(ttt))
        iris_data.append(tt)

    # iris = datasets.load_iris()
    # iris_data = iris.data
    # print(iris_data)

    lt = []
    lt.append(min(l))
    lt.append(max(l))
    iris_data.append(lt)


    # 预处理
    data = np.array(iris_data)
    min_max_scaler = preprocessing.MinMaxScaler()
    data_M = min_max_scaler.fit_transform(data)
    # print(data_M)


    ac = AgglomerativeClustering(n_clusters=60, affinity='euclidean', linkage='ward')
    ac.fit(data_M)

    labels = ac.fit_predict(data_M)
    # print(labels)

    dic = {}

    length = len(labels)-1
    for i in range(length):
        if labels[i] in dic:
            dic[labels[i]].append(data_list[i][2])
        else:
            dic[labels[i]] = []
            dic[labels[i]].append(data_list[i][2])

    # print(dic)

    # resu = []
    for i in dic.keys():
        if len(dic[i]) > 2:
            mmin = min(dic[i])
            mmax = max(dic[i])
            dic[i].remove(mmin)
            dic[i].remove(mmax)
            # resu.append((min(dic[i])+max(dic[i]))/2)
        elif len(dic[i]) == 1:
            # resu.append(dic[i][0])
            pass
        elif len(dic[i]) == 0:
            # resu.append(0)
            pass

    # print(dic)
    l_num = mean(dic[labels[length-1]])
    # print(l_num)
    return int(l_num)

